package com.cqemme.comm.utils;

import java.util.*;

/**
 * @author amkj
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils{

	/**不为空返回真***/
	public static boolean checkIsNotNull(Object param) {
		if (param instanceof String) {
			   if(param!=null&&param!="") {
			    	return true;
			    }else {
			    	return false;
			    }
		   } else {
			   if(param!=null) {
			    	return true;
			    }else {
			    	return false;
			    }
		   }
	}

	public static Map<String, Object> removeMapEmptyValue(Map<String, Object> paramMap){
		Set<String> set = paramMap.keySet();
		Iterator<String> it = set.iterator();
		List<String> listKey = new ArrayList<String>();
		while (it.hasNext()) {
		 String str = it.next();
		 if(paramMap.get(str)==null || "".equals(paramMap.get(str))||"null".equals(paramMap.get(str))){
			 		listKey.add(str) ;
		  }
		}
		 for (String key : listKey) {
		 paramMap.remove(key);
		 }
		return paramMap;
		}
	public static Map<String, String> removeStringMapEmptyValue(Map<String, String> paramMap){
		Set<String> set = paramMap.keySet();
		Iterator<String> it = set.iterator();
		List<String> listKey = new ArrayList<String>();
		while (it.hasNext()) {
			String str = it.next();
			if(paramMap.get(str)==null || "".equals(paramMap.get(str))||"null".equals(paramMap.get(str))){
				listKey.add(str) ;
			}
		}
		for (String key : listKey) {
			paramMap.remove(key);
		}
		return paramMap;
	}
	/**所有的不为空返回真***/
	public static boolean checkIsNotNulls(Object...params) {
		boolean flag=true;
		for (Object object : params) {
			if (object instanceof String) {
				   if(object==null||object=="") {
				    	flag=false;
				    }
			   } else {
				   if(object==null) {
					   flag=false;
				    }
			   }
		}

		return flag;
	}
	/**全都为空返回真***/
	public static boolean checkIsNulls(Object...params) {
		boolean flag=true;
		for (Object object : params) {
			if (object instanceof String) {
				   if(object!=null&&object!="") {
				    	flag=false;
				    }
			   } else {
				   if(object!=null) {
					   flag=false;
				    }
			   }
		}
		return flag;
	}

	/**有一个为空就返回真***/
	public static boolean checkIsNullsOne(Object...params) {
		boolean flag=false;
		for (Object object : params) {
			if (object instanceof String) {
				   if(object==null||object=="") {
				    	flag=true;
				    }
			   } else {
				   if(object==null) {
					   flag=true;
				    }
			   }
		}
		return flag;
	}
	/**有一个不为空就返回真***/
	public static boolean checkIsNotNullsOne(Object...params) {
		boolean flag=false;
		for (Object object : params) {
			if (object instanceof String) {
				   if(object!=null||object!="") {
				    	flag=true;
				    }
			   } else {
				   if(object!=null) {
					   flag=true;
				    }
			   }
		}
		return flag;
	}



	public static String composeCacheKey(String beginKey, Map<String, Object> map) {
		StringBuffer sb=new StringBuffer(beginKey);
		for (String key : map.keySet()) {
			if(StringUtils.checkIsNotNull(map.get(key))) {
				sb.append("#"+key+"="+map.get(key));
			}
		}
		return sb.toString();
	}


	/**
	 * 修改URl  /api/v1/test/xxx
	 * @param url   /api/v1/test/xxx
	 * @param delimiter
	 * @return api_v1_test_xxx
	 */
	public static String changUrl(String url, char delimiter){
		String res = "";
		if(StringUtils.isNotEmpty(url)){
			String newString = url;
			if(url.indexOf("/") == 0){ // 第一个就是 " / " 直接去除
				newString = url.substring(1);
			}
			res = newString.replaceAll("/", String.valueOf(delimiter));
		}
		return res;
	}
	/**
	 * 修改URl  /api/v1/test/xxx
	 * @param url   /api/v1/test/xxx
	 * @return api_v1_test_xxx
	 */
	public static String changUrl(String url){
		return  changUrl(url, '_');
	}
}
